from alembic import op
import sqlalchemy as sa
from sqlalchemy import Boolean, Column, Integer, String, ForeignKey, DateTime
from sqlalchemy.orm import relationship, backref
from base import Base
from admin_menu_permission import admin_menu_permission
from sqlalchemy.sql import func

# 定义模型
class AdminMenu(Base):
    __tablename__ = 'admin_menus'

    id = Column(Integer, primary_key=True, autoincrement=True, comment='主键ID')
    parent_id = Column(Integer, ForeignKey('admin_menus.id'), nullable=True, comment='父菜单ID')
    selected = Column(String(50), nullable=True, comment='选中状态')
    type = Column(Integer, nullable=False, comment='菜单类型，1, 2 或 3')
    path = Column(String(255), nullable=False, comment='菜单路径，对应前端路由路径')
    title = Column(String(255), nullable=False, comment='菜单名称，显示在界面上的文本')
    params = Column(String(255), nullable=True, comment='附加参数，以JSON字符串形式存储')
    component = Column(String(255), nullable=True, comment='前端组件路径')
    redirect = Column(String(255), nullable=True, comment='重定向路径')
    icon = Column(String(255), nullable=True, comment='菜单图标，用于界面显示的图标类名')
    is_link = Column(Boolean, default=False, comment='是否外链')
    target = Column(String(50), nullable=True, default='_self', comment='链接打开方式')
    is_tabs = Column(Boolean, default=False, comment='是否标签页显示')
    is_cache = Column(Boolean, default=False, comment='是否缓存')
    is_breadcrumb = Column(Boolean, default=True, comment='是否显示面包屑')
    is_hidden = Column(Boolean, default=False, comment='是否隐藏')
    permission_id = Column(Integer, ForeignKey('permissions.id'), nullable=True, comment='权限ID')
    sort = Column(Integer, default=0, comment='排序')
    status = Column(Integer, nullable=False, comment='状态，1 或 2')
    deleted_at = Column(DateTime(timezone=True), nullable=True, comment='删除时间')
    created_at = Column(DateTime(timezone=True), server_default=func.now(), comment='创建时间')
    updated_at = Column(DateTime(timezone=True), onupdate=func.now(), comment='更新时间')

    parent = relationship('AdminMenu', remote_side=[id])

# 定义关系
AdminMenu.permissions = relationship(
    'Permission',
    secondary=admin_menu_permission,
    back_populates='menus'
)

role_menus = relationship('SysRoleMenu', back_populates='admin_menu')

# AdminMenu.role_menus = relationship('SysRoleMenu', back_populates='admin_menu')
